﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;
using DAT = CPPEI.ETL.Domain.Data;

namespace CPPEI.ETL.WebApi.Summary
{
    [CPPEI.WebApi.AuthGetAction("Summary", "GetExtractionList", "1.0")]
    public class GetExtractionListAction : BaseAction<GetExtractionListParams, GetExtractionListResult>
    {
        protected override ActionResult<GetExtractionListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetExtractionListParams opParams)
        {
            SER.QueryService service = new SER.QueryService();
            SingleResult<DAT.ExtactSummary> result = service.Query4AppExtraction();
            if(result.IsSuccess)
            {
                GetExtractionListResult actResult = new GetExtractionListResult();
                for (int index = 0; index < result.Data.Items.Count; index++)
                {
                    GetExtractionListResult.Item taskCount = new GetExtractionListResult.Item();
                    taskCount.AppId = result.Data.Items[index].AppId;
                    taskCount.AppName = result.Data.Items[index].AppName;
                    taskCount.CreatedTime = result.Data.Items[index].CreatedTime;
                    taskCount.DataCount = result.Data.Items[index].DataCount;
                    taskCount.RejectedCount = result.Data.Items[index].RejectedCount;
                    taskCount.ExtractedCount = result.Data.Items[index].ExtractedCount;
                    actResult.Items.Add(taskCount);
                }

                return new ActionResult<GetExtractionListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetExtractionListResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
            }
        }
    }

    public class GetExtractionListParams
    {
    }

    public class GetExtractionListResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid AppId { get; set; }
            public string AppName { get; set; }
            public DateTime CreatedTime { get; set; }
            public int DataCount { get; set; }

            public int RejectedCount { get; set; }
            public int ExtractedCount { get; set; }
        }
    }
}
